package com.zy.controller;

import com.zy.constant.FileConstant;
import com.zy.service.FileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 接收对象流数据
 *
 * @author zhangyi
 * @date 2018/7/21 10:18
 */
@RestController
@RequestMapping("/connectRemote")
public class ReceiveDataController {

    @Autowired
    private FileService fileService;

    @PostMapping("/getData")
    public void getData(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String getData = "";
        Map<String, String[]> parameterMap = request.getParameterMap();
        Set<String> strings = parameterMap.keySet();
        Iterator<String> iterator = strings.iterator();
        while(iterator.hasNext()){
            getData = iterator.next();
        }
        StringBuffer stringBuffer = new StringBuffer(getData);
        if(StringUtils.contains(getData, FileConstant.C_PCB)) {
            fileService.saveData(stringBuffer.toString(), FileConstant.C_PCB);
        }else if(StringUtils.contains(getData,FileConstant.TOTAL)){
            fileService.saveData(stringBuffer.toString(), FileConstant.TOTAL);
        }
        stringBuffer.delete(0,stringBuffer.length());
    }

    @GetMapping("/test1")
    @ResponseBody
    public String test1(){
        return "测试写入！";
    }
}
